﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
    /// <summary>
    /// GetAuditTableList 的摘要说明
    /// </summary>
    public class GetAuditTableList : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //BatchNo=' + document.getElementById("<%=txtBatchNo.ClientID %>").value + '&ProjectSN=' + document.getElementById("<%=txtProjectSN.ClientID %>").value + '&ProjectName=' + document.getElementById("<%=txtProjectName.ClientID %>").value + '&ContractName=' + document.getElementById("<%=txtContractName.ClientID %>").value + '&Code=' + document.getElementById("<%=txtCode.ClientID %>").value + '&randID=' + escape(new Date()),
            string batchNo = context.Request.Params["BatchNo"];
            string projectSN = context.Request.Params["ProjectSN"];
            string projectName = context.Request.Params["ProjectName"];
            string contractName = context.Request.Params["ContractName"];
            string code = context.Request.Params["Code"];
            int pageSize=Convert.ToInt32(context.Request.Params["PageSize"]);
            int currentPageIndex=Convert.ToInt32(context.Request.Params["CurrentPageIndex"]);
            int pageCount=0,recordCount=0;
            bool? isBF=null,isGive=null;
            if(context.Request.Params["IsBF"] != "0")
            {
                isBF = context.Request.Params["IsBF"] == "2";
            }
            if(context.Request.Params["IsGive"] != "0")
            {
                isGive = context.Request.Params["IsGive"] == "2";
            }
            int? batch=null;
            if(batchNo !=""){batch = Convert.ToInt32(batchNo);}
            DataTable dt = new Honestysoft.Financing.BusinessLogic.Audit("").GetBFGive(batch, projectSN, projectName, contractName, code, isGive, isBF, pageSize, currentPageIndex, out pageCount, out recordCount);
            string json = "{\"total\":" + dt.Rows.Count + ",\"rows\":[";
            //A.ID,B.SN ProjectSN,A.BatchNo,A.ProjectName,A.ContractName,A.Code,A.Amount_DocRec,A.OwnerName,A.ContractorName,A.Amount_S,A.IsGive,A.GiveAmount,A.GiveDate,A.IsBF,A.BFAmount,A.BFDate
            foreach (DataRow dr in dt.Rows)
            {
                json += "{";
                json += "\"ID\":\"" + dr["ID"].ToString() + "\",";
                json += "\"ProjectSN\":\"" + dr["ProjectSN"].ToString() + "\",";
                json += "\"BatchNo\":\"" + dr["BatchNo"].ToString() + "\",";
                json += "\"ProjectName\":\"" + dr["ProjectName"].ToString() + "\",";
                json += "\"ContractName\":\"" + dr["ContractName"].ToString() + "\",";
                json += "\"Code\":\"" + dr["Code"].ToString() + "\",";
                json += "\"OwnerName\":\"" + dr["OwnerName"].ToString() + "\",";
                json += "\"ContractorName\":\"" + dr["ContractorName"].ToString() + "\",";
                json += "\"ProjectName\":\"" + dr["ProjectName"].ToString() + "\",";
                json += "\"Amount\":\"" + string.Format("{0:0.00}", dr["Amount"]) + "\",";
                json += "\"IsChecked\":\"" + (Convert.ToBoolean(dr["IsChecked"]) == true ? "√" : "") + "\",";
                json += "\"Remark\":\"" + dr["Remark"].ToString() + "\"";
                json += "},";
            }
            if (json.EndsWith(","))
            {
                json = json.Remove(json.Length - 1);
            }
            json += "]}";
            context.Response.Write(json);
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}